<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="用户">
                <AWM-select
                  v-model="queryParam.userId"
                  placeholder="请输入选择用户"
                  funStr="userListByNoAuth"
                ></AWM-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced"> </template>
            <!-- <a-col :md="(!advanced && 8) || 24" :sm="24"> -->
            <a-col :md="8" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (this.queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <!-- <a-button type="primary" icon="plus" @click="handleAdd"  v-action:financeLog_add>新建</a-button> -->
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
        :scroll="{ x: 'auto' }"
      >
        <span slot="param" slot-scope="text">
          <div class="param-wrap">{{ text }}</div>
        </span>
        <span slot="ft" slot-scope="text, record">
          {{ `${record.fromNum || '-'} - ${record.endNum || '-'}` }}
        </span>
        <span slot="status" slot-scope="text">
          <a-tag :color="text | statusFilter(financeStatusList, 'status')">{{
            text | statusFilter(financeStatusList, 'name')
          }}</a-tag>
        </span>
        <span slot="atta" slot-scope="text">
          <template>
            <AWM-file-list :list="text"></AWM-file-list>
          </template>
        </span>
        <span slot="remark" slot-scope="text">
          <template>
            <div class="remark-wrap">{{ text }}</div>
          </template>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a @click="handleDel(record)">删除</a>
            <!-- <a-divider type="vertical" />
            <a @click="handleMakeInvoice(record)" v-if="record.invoiceStatus == 0">开票</a>
            <a @click="checkInvoice(record)" v-else>开票信息</a> -->
          </template>
        </span>
      </s-table>
      <!-- IncomeForm -->
      <sms-curtail-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
// import {  eachMap, financeStatusList } from '@/utils/other'
import SmsCurtailForm from './modules/SmsCurtailForm'
import AWMSelect from '@/components/AWMSelect'
import { getSmsCurtailList, getSmsCurtailAdd, getSmsCurtailUpdate } from '@/api/sms'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 100,
    key: 'id',
  },
  {
    title: '用户',
    dataIndex: 'userName',
    width: '200px',
    customRender: (text, record) => `${text} (${record.userId})`,
  },
  {
    title: '数量范围',
    dataIndex: 'ft',
    width: '200px',
    scopedSlots: { customRender: 'ft' },
  },
  {
    title: '缩减比例',
    width: '200px',
    dataIndex: 'rate',
    customRender: (text) => text + ' %' || '--',
  },
  {
    title: '添加人',
    width: 200,
    dataIndex: 'addByName',
  },
  {
    title: '添加时间',
    dataIndex: 'addTime',
    width: 200,
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name: 'AWM-TypeList',
  components: {
    STable,
    SmsCurtailForm,
    AWMSelect,
  },
  data() {
    this.columns = columns
    return {
      open: false,
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      details: {
        visible: false,
        confirmLoading: false,
        id: null,
      },
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      parameter: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParam: {},
      data: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        console.log(this.queryParam);
        const requestParameters = Object.assign( parameter, this.queryParam)
        return getSmsCurtailList(requestParameters).then((res) => {

          let result = {
            data: res.data.data,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalCount: res.data.total,
            totalPage: res.data.pages,
          }
          return result
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  created() {
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange,
      }
    },
  },
  methods: {
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    saveCB() {
      const form = this.$refs.createModal.form
      this.visible = false
      this.confirmLoading = false
      // 重置表单数据
      form.resetFields()
      // 刷新表格
      this.$refs.table.refresh()
    },
    handleOk() {
      const createModal = this.$refs.createModal
      const form = createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          if (values.id > 0) {
            getSmsCurtailUpdate(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('修改成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          } else {
            delete values.id
            getSmsCurtailAdd(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('新增成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },

    handleDel(a_record) {
      let self = this
      const modal = self.$confirm({
        title: '用户缩减删除',
        content: `是否删除用户缩减:${a_record.userName}: ${a_record.fromNum}-${a_record.endNum}`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => self.handleDelete(a_record.id, modal),
        onCancel: () => {},
      })
    },

    handleDelete(a_id, a_modal) {
      return new Promise((resolve, reject) => {
        let data = { id: a_id, status: -1 }
        getSmsCurtailUpdate(data)
          .then((res) => {
            this.$refs.table.refresh()
            this.$message.success('删除成功!')
            resolve()
          })
          .catch((err) => {
            a_modal.destroy()
            this.$message.error('操作失败!')
          })
      })
    },
  },
}
</script>
<style scoped lang="less">
.param-wrap {
  min-width: 200px;
  max-width: 300px;
}

.a-tag-wrap {
  font-size: 12px;
  /deep/ .ant-tag {
    line-height: 18px;
    padding: 0 4px;
  }
  div {
    margin-bottom: 2px;
    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>



 